{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a href=\"https://colab.research.google.com/github/run-llama/llama_index/blob/main/docs/examples/embeddings/cloudflare_workersai.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Cloudflare Workers AI Embeddings"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Setup\n",
    "\n",
    "Install library via pip"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install llama-index-embeddings-cloudflare-workersai\n",
    "# %pip install -e ~/llama_index/llama-index-integrations/embeddings/llama-index-embeddings-cloudflare-workersai"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To acess Cloudflare Workers AI, both Cloudflare account ID and API token are required. To get your account ID and API token, please follow the instructions on [this document](https://developers.cloudflare.com/workers-ai/get-started/rest-api/)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Initilise with account ID and API token\n",
    "\n",
    "# import os\n",
    "\n",
    "# my_account_id = \"example_id\"\n",
    "# my_api_token = \"example_token\"\n",
    "# os.environ[\"CLOUDFLARE_AUTH_TOKEN\"] = \"my_api_token\"\n",
    "\n",
    "import getpass\n",
    "\n",
    "my_account_id = getpass.getpass(\"Enter your Cloudflare account ID:\\n\\n\")\n",
    "my_api_token = getpass.getpass(\"Enter your Cloudflare API token:\\n\\n\")"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Text embeddings example"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "384\n",
      "[-0.04786296561360359, -0.030788540840148926, -0.07126234471797943, -0.04107927531003952, 0.02904760278761387]\n"
     ]
    }
   ],
   "source": [
    "from llama_index.embeddings.cloudflare_workersai import CloudflareEmbedding\n",
    "\n",
    "my_embed = CloudflareEmbedding(\n",
    "    account_id=my_account_id,\n",
    "    auth_token=my_api_token,\n",
    "    model=\"@cf/baai/bge-small-en-v1.5\",\n",
    ")\n",
    "\n",
    "embeddings = my_embed.get_text_embedding(\"Why sky is blue\")\n",
    "\n",
    "print(len(embeddings))\n",
    "print(embeddings[:5])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Embed in batches"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "As for batch size, Cloudflare's limit is a maximum of 100, as seen on 2024-03-31."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "384\n",
      "[-0.04786296561360359, -0.030788540840148926, -0.07126234471797943, -0.04107927531003952, 0.02904760278761387]\n",
      "[-0.08951402455568314, -0.015274363569915295, 0.04728245735168457, 0.05478525161743164, 0.05978189781308174]\n"
     ]
    }
   ],
   "source": [
    "embeddings = my_embed.get_text_embedding_batch(\n",
    "    [\"Why sky is blue\", \"Why roses are red\"]\n",
    ")\n",
    "print(len(embeddings))\n",
    "print(len(embeddings[0]))\n",
    "print(embeddings[0][:5])\n",
    "print(embeddings[1][:5])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3"
  },
  "vscode": {
   "interpreter": {
    "hash": "64bcadabe4cd61f3d117ba0da9d14bf2f8e35582ff79e821f2e71056f2723d1e"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
